﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Algorithms.Bidirectional;
using Algorithms.Depth;
using Algorithms.IDepthSearch;
using Algorithms.WidthSearch;
using Operators;
using Operators.CMOperators;
using Operators.WaterCupOperators;
using Satates;
using Satates.CMStates;
using Satates.FarmerStates;
using Satates.WaterCupState;
using  Arbore;
using Operators.FarmerOperators;
using Algorithms.LDepthStrategy;
using Algorithms;
using Algorithms.HillClimbing;
using PresentationWpf.Iteraror;
using PresentationWpf.Memento;
using PresentationWpf.TemplateMethod;

namespace PresentationWpf
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        int mementoCounter;
        SelectedIterator SelIterator; 
        Arbore.Arbore arb;
        bool swicher;
       
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            mementoCounter = 0;
            swicher = false;
           Context w = new Context(new BidirectionalStrategy());
            State waterCupInitialState = FarmerStates.initalState();
            List<State> waterCupFinalState = FarmerStates.finalStates();
            List<iOperator> waterCupOperators = FarmerOperators.returnOperators();
            List<string> result = new List<string>();
            Arbore.Arbore ab = new Arbore.Arbore();
            w.executeStrategy(waterCupInitialState, waterCupFinalState, waterCupOperators, ref  result, ref ab);
            textBlock1.Text = resultToString.listToString(result);
            SelIterator = new SelectedIterator();

            viewer1.setArbore(ab);
            arb = ab;
        }

        private List<string> listaEXI;
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if(comboBox1.Text!=""&&comboBox2.Text!=""){
                execute(comboBox2.Text, comboBox1.Text);
            }


        }
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            UIElement[] ty = new UIElement[300];
         //   Tree.Children.CopyTo(ty, 0);
            TreeViewer t = new TreeViewer(ref arb);
            t.Show();
        }

        private void comboBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            mm = new MementoMemory();
            string text = (e.AddedItems[0] as ComboBoxItem).Content as string;
            swicher = true;
            switch (text)
            {
               
                case "WaterCups": comboBox1.Items.Clear();
                     comboBox1.Items.Add("DepthStrategy");
                     comboBox1.Items.Add("WidthStrategy");
                     comboBox1.Items.Add("LDepthStrategy");
                     comboBox1.Items.Add("IDepthStrategy");
                     comboBox1.Items.Add("hill");
                     break;
                case "Farmer":
                        comboBox1.Items.Clear();
                        comboBox1.Items.Add("DepthStrategy");
                        comboBox1.Items.Add("WidthStrategy");
                        comboBox1.Items.Add("LDepthStrategy");
                        comboBox1.Items.Add("IDepthStrategy");
                        comboBox1.Items.Add("BidirectionalStrategy");
                         break;
                case "CM" :
                          comboBox1.Items.Clear();
                        comboBox1.Items.Add("DepthStrategy");
                        comboBox1.Items.Add("WidthStrategy");
                        comboBox1.Items.Add("LDepthStrategy");
                        comboBox1.Items.Add("IDepthStrategy");
                        comboBox1.Items.Add("BidirectionalStrategy");
                         break;
                default:comboBox1.Items.Clear(); break;
            }
            swicher = false;
        }

        public void execute(string state,string strategy){       
             List<string> result = new List<string>();     
             try
             {
                 var Ab = new Arbore.Arbore();
                 AbstractClass concreteTemplate = new ConcreteTemplate();
                 concreteTemplate.TemplateMethod(ref result, ref Ab, state, strategy);
               
                 textBlock1.Text = resultToString.listToString(result);

                 tabItem2.Visibility = Visibility.Visible;
                 var V = new Viewer();
                 V.setArbore(Ab);
                 gr.Children.Clear();
                 V.VerticalAlignment = VerticalAlignment.Top;
                 V.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                 gr.Children.Add(V);
                 arb = Ab;
                 if (mementoCounter == 0)
                 {
                     s = new Organizator();
                     s._ab=arb;
                     s._u = V;
                     s._strategy = strategy;
                     s._state = state;
                     s._content = textBlock1.Text;
                     mementoCounter++;
                 }else
                 {
                     mm.Memento=s.SaveMemento();
                     s._ab = arb;
                     s._u = V;
                     s._strategy = strategy;
                     s._state = state;
                     s._content = textBlock1.Text;
                     button5.IsEnabled = true;
                 }
             }
             catch
             {
                 gr.Children.Clear();
                 Eroare V = new Eroare();
                 V.VerticalAlignment = VerticalAlignment.Top;
                 tabItem2.Visibility = Visibility.Hidden;
                 V.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                 gr.Children.Add(V);
             }
             
        }

        private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!swicher)
            {
                string text = e.AddedItems[0].ToString();
                if (text != "" && comboBox2.Text != "")
                {
                    SelIterator.Add(comboBox2.Text, text);
                }
            }
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            swicher = true;
            var i = SelIterator.Undo();
            if (i != null)
            {
                comboBox2.Text = i.State;
                comboBox1.Text = i.Strategy;
            }
            swicher = false;
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            swicher = true;
            var i = SelIterator.Next();
            if (i != null)
            {
                comboBox2.Text = i.State;
                comboBox1.Text = i.Strategy;
            }
            swicher = false;
        }
        Organizator s;
        MementoMemory mm;
        private void button5_Click(object sender, RoutedEventArgs e)
        {
            s.RestoreMemento(mm.Memento);
            gr.Children.Clear();
            gr.Children.Add(s._u);
            textBlock1.Text = s._content;
            swicher = true;
            arb = s._ab;
            comboBox2.Text = s._state;
            comboBox1.Text = s._strategy;
            swicher = false;
            button5.IsEnabled = false;
        }

    }
}
